const base = require("../base/controllerBase");
const moment = require("moment");
const _ = require("lodash");

class MeetingController extends base {

    async createMeetingRecord() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                tenantCode: {type: 'string'}
            };
            ctx.validate(model, body);
            body.code = 'MRD' + moment().format('YYYYMMDDHHmmss');
            const result = await ctx.app.dbs.insert('MeetingRecord', body);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async deleteMeetingRecord() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                tenantCode: {type: 'string'},
                code: {type: 'string'}
            };
            ctx.validate(model, body);
            const {code, tenantCode} = body;
            const result = await ctx.app.dbs.deleteOne('MeetingRecord', {code, tenantCode});
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async updateMeetingRecord() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                tenantCode: {type: 'string'},
                code: {type: 'string'}
            };
            ctx.validate(model, body);
            const {code, tenantCode} = body;
            const result = await ctx.app.dbs.updateOne('MeetingRecord', {code, tenantCode}, body);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findMeetingRecordMany() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                tenantCode: {type: 'string'}
            };
            ctx.validate(model, body);
            const {tenantCode, time} = body;

            const query = {tenantCode};
            if (time && time.length === 2) {
                const start = moment(time[0]).valueOf();
                const end = moment(time[1]).valueOf();
                query['time'] = {$gt: start, $lt: end};
            }
            const result = await ctx.app.dbs.find('MeetingRecord', query);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findMeetingRoomMany() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                tenantCode: {type: 'string'}
            };

            ctx.validate(model, body);
            const result = ['凌云厅'];
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

}

module.exports = MeetingController;
